#{extends '/template/main.html' /}
<!-- treeTable -->
<link rel="stylesheet" href="/public/treeTable/default/jquery.treeTable.css">
<link rel="stylesheet" href="/public/treeTable/vsStyle/jquery.treeTable.css">
<script src="/public/treeTable/jquery.treeTable.js"></script>
<script src="/public/js/ulmsale/common.js"></script>
<script src="/public/lib/layer/js/layer.js"></script>

<script>

    $(function () {
        // 提示
        var msg = '${flash.get("msg")}';
        if(msg) {
            saveSuccess(msg);
        }

        getAsyncJSON("/power/admin/AdminDeptCtrl/loadTreeData", {id:0}, function (data) {
            initTree(data);

        });

    });

    function initTree(treeList) {
        $('#deptTree').html('');

        var trHtml  = ' <thead> <tr> <td>部门名称</td> <td>部门编号</td> <td>排序号</td> <td>备注</td> <td>操作</td> </tr> </thead>';
        for(var i = 0 ; i < treeList.length ; i++){
            var trData = treeList[i];
            var hasChild = (trData.isLeaf==true || trData.isLeaf == 1 )? '': true;
            if(trData.parentId == 0){
                trHtml += '<tr id="'+trData.id+'"  hasChild="'+hasChild+'"> ';
                trHtml += '<td id="name_'+trData.id+'"><span controller="true">'+trData.name+'</span></td>';
                trHtml += '<td id="code_'+trData.id+'">'+trData.code+'</td>';
                trHtml += '<td id="showOrder_'+trData.id+'">'+trData.showOrder+'</td>';
                trHtml += '<td id="remark_'+trData.id+'">'+trData.remark+'</td>';
                trHtml += '<td><button class="btn btn-success" onclick="addNode('+trData.id+');">子级</button>';
                trHtml += '<button class="btn btn-info" onclick="editNode('+trData.id+','+trData.parentId+');">编辑</button></td>';
                trHtml += '</tr>';
            }else {
                trHtml += '<tr id="'+trData.id+'" hasChild="'+hasChild+'" name="pid_'+trData.parentId+'"  pid="'+trData.parentId+'"> ';
                trHtml += '<td id="name_'+trData.id+'">'+trData.name+'</td>';
                trHtml += '<td id="code_'+trData.id+'">'+trData.code+'</td>';
                trHtml += '<td id="showOrder_'+trData.id+'">'+trData.showOrder+'</td>';
                trHtml += '<td id="remark_' +trData.id+'">'+trData.remark+'</td>';
                trHtml += '<td><button class="btn btn-secondary" onclick="addNode('+trData.parentId+');">同级</button>';
                trHtml += '<button class="btn btn-success" onclick="addNode('+trData.id+');">子级</button>';
                trHtml += '<button class="btn btn-info" onclick="editNode('+trData.id+','+trData.parentId+');">编辑</button>';
                trHtml += '<button class="btn btn-pink" onclick="deletedComfim('+trData.id+');">删除</button></td>';
                trHtml += '</tr>';

            }

        }
        $('#deptTree').append(trHtml);

        //初始化表格样式
        var option = {
            theme:'vsStyle',
            expandLevel :2 ,
            onSelect: function ($treeTable, id) {
                window.console && console.log('onSelect:' + id);
            },
            beforeExpand:function ($treeTable, id) {
                $.getJSON('/power/admin/AdminDeptCtrl/loadTreeData', {id: id}, function (data) {
                    var trHtml = '';
                    if(data != null && data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            var trData = data[i];
                            var hasChild = (trData.isLeaf == true || trData.isLeaf == 1 ) ? '' : true;
                            trHtml += '<tr id="' + trData.id + '" hasChild="'+hasChild+'" name="pid_'+trData.parentId+'"  pid="'+trData.parentId+'"> ';
                            trHtml += '<td id="name_'+trData.id+'">' + trData.name + '</td>';
                            trHtml += '<td id="code_'+trData.id+'">' + trData.code + '</td>';
                            trHtml += '<td id="showOrder_'+trData.id+'">'+trData.showOrder+'</td>';
                            trHtml += '<td id="remark_'+trData.id+'">' + trData.remark + '</td>';
                            trHtml += '<td><button class="btn btn-secondary" onclick="addNode('+trData.parentId+');">同级</button>';
                            trHtml += '<button class="btn btn-success" onclick="addNode('+trData.id+');">子级</button>';
                            trHtml += '<button class="btn btn-info" onclick="editNode('+trData.id+','+trData.parentId+');">编辑</button>';
                            trHtml += '<button class="btn btn-pink" onclick="deletedComfim('+trData.id+');">删除</button></td>';
                            trHtml += '</tr>';
                        }
                        $treeTable.addChilds(trHtml , 1);
                    }

                })
            }

        }
        $('#deptTree').treeTable(option);
    }

    //添加同级节点
    function addNode( parentId) {
        $('#id_parent_id').val(parentId);
        $('#id_dept_id').val('');
        $('#id_dept_name').val('');
        $('#id_dept_code').val('');
        $('#id_show_order').val('');
        $('#id_dept_remark').val('');
        $('#adminDeptModal').modal('show');

    }
//    //添加子级菜单
//    function addChildNode(id) {
//        $('#id_parent_id').val(id);
//        $('#id_dept_id').val('');
//        $('#id_dept_name').val('');
//        $('#id_dept_code').val('');
//        $('#id_show_order').val('');
//        $('#id_dept_remark').val('');
//        $('#adminDeptModal').modal('show');
//    }

    //编辑节点
    function editNode(id , parentId) {
        var showOrder =Number( $('#showOrder_'+id).text());
        $('#id_parent_id').val(parentId);
        $('#id_dept_id').val(id);
        $('#id_dept_name').val($('#name_'+id).text());
        $('#id_dept_code').val($('#code_'+id).text());
        $('#dept_id_show_order').val(showOrder);
        $('#id_dept_remark').val($('#remark_'+id).text());
        $('#adminDeptModal').modal('show');
    }
    //删除
    function sureDeleted() {
        //获取删除的id
        var id =$('#deleteId').val();
        document.location.href='/power/admin/AdminDeptCtrl/deletedDept?id='+id;

    }

    function saveDeptDetail() {
        $('#adminDeptForm').submit();

    }
</script>

<div class="panel panel-default">

    <div class="panel-heading">
        <h3 class="panel-title">
            <button id="idBtnSave" class="btn btn-secondary" onclick="addNode(0);">创建根目录</button>
            *{<button id="idBtnSave" class="btn btn-success" onclick="editMenuItem(1);">新建子菜单</button>}*
            *{<button id="idBtnEdit" class="btn btn-info" onclick="editMenuItem(3);">查看/编辑</button>}*
            *{<button id="idBtnDelete" class="btn btn-pink" onclick="deletedComfim();">删除</button>}*
        </h3>

        <div class="panel-body">
            <table id="deptTree" class="table table-bordered table-striped" style="text-align:left; width: 100%">

            </table>
        </div>
    </div>
</div>




